﻿package net.pezzato.graphics {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import net.pezzato.graphics.Widget;
	public class TitleBar extends Widget {
		private var bg_:Sprite;
		private var tf_:TextField;
		private var fo_:TextFormat;
		private var minWidth_:Number;
		private static const DEFAULT_HEIGHT:Number = 20;
		public function TitleBar(widgetParent:Widget, title:String, w:Number) {
			super(widgetParent, "TitleBar");
			dispatchChange_ = false;
			/* draw background */
			bg_ = new Sprite();
			addChild(bg_);
			/* draw text */
			fo_ = new TextFormat("Arial", DEFAULT_HEIGHT*0.8, 0xffffff, true, false, false, null, null, "left");
			tf_ = new TextField();
			tf_.defaultTextFormat = fo_;
			tf_.autoSize = "left";
			tf_.selectable = false;
			tf_.text = title;
			tf_.x = 5;
			minWidth_ = tf_.width;
			changeWidth(w);
			addChild(tf_);
			dispatchChange_ = true;
			changed();
		}
		public function changeWidth(w:Number) {
			if (w < minWidth_) {
				drawBg(minWidth_, DEFAULT_HEIGHT);
			} else {
				drawBg(w, DEFAULT_HEIGHT);
			}
		}
		private function drawBg(w, h) {
			bg_.graphics.clear();
			bg_.graphics.beginFill(0x666666, 1);
			bg_.graphics.drawRect(0, 0, w, h);
			changed();
		}
	}
}